
//自定义单向链表
public class SingleLinkedList {
    static  class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    //初始化链表数据
    public void FirstListNode(){
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(1);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        this.head= listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        listNode5.next = null;

    }
    //头插法
    public void addFirst(int data){
        ListNode node0 = new ListNode(data);
        if(head == null){
            head = node0;
            node0.next = null;
            return;
        }
        node0.next = head;
        head = node0;

    }
    //尾插法
    public void addLast(int data){
        ListNode node0 = new ListNode(data);
        //定义cur 用来遍历链表 保证head结点不动
        ListNode cur = head;
        if (head == null){
            head = node0;
            node0.next = null;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node0;
        node0.next = null;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            node.next = null;
            return;
        }
        try{
                FindExprction(index);
                ListNode cur = head;
                //头插
                if (index == 0){
                    addFirst(data);
                    return;
                }
                //尾插
                if (index == size() - 1){
                    addLast(data);
                    return;
                }

                while(index != 0) {
                    cur = cur.next;
                    index--;
                }
                node.next = cur.next;
                cur.next = node;

            }catch (IndexExpection e){
                e.printStackTrace();
                System.out.println("接收到IndexExpection异常");
            }
    }
    public  void FindExprction(int index) throws IndexExpection{
        ListNode cur = head;
        int num  = size() - 1;
        if(index < 0 || index > num){
            throw new IndexExpection("下标有误");
        }

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){

        if (head == null){
            return ;
        }
        //key为头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = head;
        ListNode del =head.next;
        //使del为要删的结点 cur为要删的前一结点
        while (del.next != null){
            if (del.val == key){
                break;
            }
            cur = cur.next;
            del=  del.next;
        }
        //key为尾部
        if (del.next == null){
            cur.next = null;
            return;
        }
        //key在中间位置
        cur.next = del.next;


    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode del = head.next;
        ListNode cur = head;
        while (del != null){
             if (del.val == key){
                cur.next = del.next;
                del = del.next;
            }
             //可能连着为key，要等del不为key时在把cur向后传递，不然会导致少删除一个key
            else {
                cur = del;
                del = del.next;
            }
        }
        //处理头结点为key的情况
        if(head.val == key){
            head = head.next;
            return;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        if(cur == null) return -1;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear() {

        this.head = null;

    }


    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;

        }
        System.out.println();
    }
}